﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mall.Core;
using Mall.Data;
using Solution;
using Webdiyer.WebControls.Mvc;

namespace Mall.AdminSystem.Controllers
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    public class MenuController : BaseController
    {
        private MenuActionCore maCore = new MenuActionCore();

        // GET: Menu
        public ActionResult Index(int id = 0)
        {
            var plist = maCore.LoadEntities(o => o.A_State == 0).ToList();
            var rs = MenuTreeStr(plist.Where(o => o.A_Flag == 1 && o.A_PId == 0), plist);
            string rsStr = Serializer.ToJson(rs);
            ViewBag.MenuStr = rsStr;
            return View();
        }
        /// <summary>
        /// 递归遍历 所有菜单/功能
        /// </summary>
        /// <param name="plist">当前父节点遍历</param>
        /// <param name="allList">所有角色菜单</param>
        /// <returns></returns>
        private dynamic MenuTreeStr(IEnumerable<Sys_MenuAction> plist, IEnumerable<Sys_MenuAction> allList)
        {
            var rs = plist.Select(o => new
            {
                name =string.IsNullOrEmpty(o.A_Icon)? o.A_Title:
                    $"<i class='ace-icon fa {o.A_Icon} blue'></i> {o.A_Title}",
                type = o.A_Flag == 0 ? "item" : "folder",
                id = o.A_Id,
                additionalParameters = new
                {
                    children = MenuTreeStr(allList.Where(a => a.A_PId == o.A_Id), allList)
                }
            });
            return rs;
        }
        /// <summary>
        /// 查询菜单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult QueryMenyById(int id)
        {
            var info = maCore.LoadEntity(o => o.A_Id == id);
            if (info == null)
            {
                return Json("error");
            }
            return Json(new {  info.A_Id, info.A_PId, info.A_Title,  info.A_State, info.A_Flag, info.A_Controller,  info.A_Action, info.A_Memo,  info.A_Icon, info.A_Rank });
        }
        /// <summary>
        /// 保存管理员
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Sys_MenuAction obj)
        {
            obj.A_Time = DateTime.Now;
            if (obj.A_Id == 0) //新增
            {
                maCore.AddEntity(obj);
            }
            else //修改
            {
                maCore.Update(obj);
            }

            return RedirectToAction("Index");
        }
        /// <summary>
        /// 冻结/解冻
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Forzen(int id)
        {

            StandardJsonResult rs = new StandardJsonResult();
            var info = maCore.LoadEntity(o => o.A_Id == id);

            if (info != null)
            {
                var state = info.A_State;
                if (state == 0)
                {
                    state = 1;
                }
                else if (state == 1)
                {
                    state = 0;
                }
                info.A_State = state;
                if (maCore.Update(info)) //更改状态
                {
                    rs.State = 0;
                }
            }
            return Json(rs, JsonRequestBehavior.AllowGet);
        }

    }
}